package com.example.screenapppush


import android.annotation.SuppressLint
import android.media.projection.MediaProjection
import android.util.Log
import org.java_websocket.WebSocket
import org.java_websocket.handshake.ClientHandshake
import org.java_websocket.server.WebSocketServer
import java.lang.Exception
import java.net.InetSocketAddress
import java.net.Socket

class SocketLive()  :WebSocketServer(InetSocketAddress(9088)){

     var  socket:WebSocket ?=null;
    var mediaProjection: MediaProjection ?= null
    fun startWebSocketServer() {
        // 开始 WebSocketServer
        this.start()
    }
    fun startLive() {

        var h264ScreenEncoder = H264ScreenEncoder(mediaProjection!!)
        h264ScreenEncoder.startEncode(){
            sendData(byteArray = it)
        }
    }
    fun sendData(byteArray: ByteArray) {
        Log.e("发送数据", "${byteArray.size}")
        socket?.send(byteArray)
    }

    @SuppressLint("LongLogTag")
    override fun onOpen(conn: WebSocket?, handshake: ClientHandshake?) {
        Log.e("启动WebSocketServer---onOpen", handshake.toString())
        if (conn != null) {
            socket = conn
        }
    }

    @SuppressLint("LongLogTag")
    override fun onClose(conn: WebSocket?, code: Int, reason: String?, remote: Boolean) {
        Log.e("启动WebSocketServer-onClose", reason.toString())
    }

    @SuppressLint("LongLogTag")
    override fun onMessage(conn: WebSocket?, message: String?) {
        Log.e("启动WebSocketServer---onMessage", message.toString())
    }

    @SuppressLint("LongLogTag")
    override fun onError(conn: WebSocket?, ex: Exception?) {
        Log.e("启动WebSocketServer----onError", ex.toString())
    }

    @SuppressLint("LongLogTag")
    override fun onStart() {
        Log.e("启动WebSocketServer-onStart", "onStart")
    }


}